//
//  ByShapeModel.m
//  laserdevice_ios
//
//  Created by pro on 16/11/26.
//  Copyright © 2016年 com.itboye. All rights reserved.
//

#import "ByShapeModel.h"

@implementation ByShapeModel

// 用于保存的字典
- (NSMutableDictionary *)saveDictionary {
    
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    
    NSArray *typeAry = @[@"line",
                         @"angle",
                         @"triangle",
                         @"square",
                         @"trapezoid",
                         @"cuboids",];
    [dic setObject:typeAry[self.mode] forKey:@"ele_type"];  // 图形类型
    [dic setObject:self.name forKey:@"name"];               // 图形名称
    NSMutableArray *points = [[NSMutableArray alloc] init];
    if (self.mode != ByShapeModeCuboids) {
        for (NSValue *pointValue in self.points) {
            CGPoint point = [pointValue CGPointValue];
            NSDictionary *pointDic = @{@"x":[NSString stringWithFormat:@"%.1lf", point.x],
                                       @"y":[NSString stringWithFormat:@"%.1lf", point.y],};
            [points addObject:pointDic];
        }
    } else {
        for (NSValue *pointValue in self.cuboidsDrawUpPoints) {
            CGPoint point = [pointValue CGPointValue];
            NSDictionary *pointDic = @{@"x":[NSString stringWithFormat:@"%.1lf", point.x],
                                       @"y":[NSString stringWithFormat:@"%.1lf", point.y],};
            [points addObject:pointDic];
        }
        for (NSValue *pointValue in self.cuboidsDrawDownPoints) {
            CGPoint point = [pointValue CGPointValue];
            NSDictionary *pointDic = @{@"x":[NSString stringWithFormat:@"%.1lf", point.x],
                                       @"y":[NSString stringWithFormat:@"%.1lf", point.y],};
            [points addObject:pointDic];
        }
    }
    [dic setObject:points.copy forKey:@"points"];           //
    [dic setObject:self.attributes forKey:@"attributes"];   //
    
    NSArray *unitAry = @[@"m",
                         @"ft",
                         @"in",
                         @"ft+in",];
    int unit = [ByCommonCache getSystemUnit].intValue;
    [dic setObject:unitAry[unit] forKey:@"unit"];                 // 单位

    return dic;
}

// 用字典生成model
- (instancetype)initWithJsonDic:(NSDictionary *)dic sceneName:(NSString *)sceneName {
    
    if (self = [super init]) {
        
        NSArray *typeAry = @[@"line",
                             @"angle",
                             @"triangle",
                             @"square",
                             @"trapezoid",
                             @"cuboids",];
        NSString *type = dic[@"ele_type"];
        ByShapeMode mode = [typeAry indexOfObject:type];
        self.mode = mode;
        [self initViews];
        [self initPoints];

        self.name = dic[@"name"];
        NSString *imagePath = dic[@"path"];
        if (imagePath.length != 0) {
            self.picture = [ByFileManager getImageAtScene:sceneName imagePath:imagePath];
        }
        
        NSArray *unitAry = @[@"m",
                             @"ft",
                             @"in",
                             @"ft+in",];
        int unit = (int)[unitAry indexOfObject:dic[@"unit"]];
        NSMutableArray *attributesTemp = [[NSMutableArray alloc] init];
        NSArray *as = dic[@"attributes"];
        
        for (int index = 0; index < as.count; index ++) {
            NSString *attstr = as[index];
        
//        for (NSString *attstr in dic[@"attributes"]) {
        
            CGFloat value = [attstr floatValue];
            if (value == 0) {   // 未设置
                [attributesTemp addObject:attstr];
            } else {
//                NSInteger index = [dic[@"attributes"] indexOfObject:attstr];
//                NSInteger count = [dic[@"attributes"] count];
                NSInteger count = as.count;
                // 先把数据转成 m
                switch (unit) {
                    case 0: // 米
                        value = value;
                        break;
                    case 1: // 英尺
                        if (index == count - 1) {
                            if (mode == ByShapeModeLine) {
                                value /= pow(3.2808399, 1);
                            }
                            else if (mode == ByShapeModeAngle) {
                                value /= pow(3.2808399, 1);
                            }
                            else if (mode != ByShapeModeCuboids) {
                                value /= pow(3.2808399, 2);
                            } else {
                                value /= pow(3.2808399, 3);
                            }
                        }
                        else if (index == 0 && mode == ByShapeModeAngle) {
                            value = value;
                        }
                        else {
                            value /= 3.2808399;
                        }
                        break;
                    case 2: // 英寸
                        if (index == count - 1) {
                            if (mode == ByShapeModeLine) {
                                value /= pow((3.2808399 * 12), 1);
                            }
                            else if (mode == ByShapeModeAngle) {
                                value /= pow(3.2808399 * 12, 1);
                            }
                            else if (mode != ByShapeModeCuboids) {
                                value /= pow((3.2808399 * 12), 2);
                            } else {
                                value /= pow((3.2808399 * 12), 3);
                            }
                        }
                        else if (index == 0 && mode == ByShapeModeAngle) {
                            value = value;
                        }
                        else {
                            value /= (3.2808399 * 12);
                        }
                        break;
                    case 3: // 英尺+英寸
                        if (index == count - 1) {
                            if (mode == ByShapeModeLine) {
                                value /= pow(3.2808399, 1);
                            }
                            else if (mode == ByShapeModeAngle) {
                                value /= pow(3.2808399, 1);
                            }
                            else if (mode != ByShapeModeCuboids) {
                                value /= pow(3.2808399, 2);
                            } else {
                                value /= pow(3.2808399, 3);
                            }
                        }
                        else if (index == 0 && mode == ByShapeModeAngle) {
                            value = value;
                        }
                        else {
                            value /= 3.2808399;
                        }
                        break;
                    default:
                        break;
                }
                
                // 在吧数据转成我需要的
                switch ([ByCommonCache getSystemUnit].intValue) {
                    case 0: // 米
                        value = value;
                        break;
                    case 1: // 英尺
                        if (index == count - 1) {
                            if (mode == ByShapeModeLine) {
                                value *= pow((3.2808399), 1);
                            }
                            else if (mode == ByShapeModeAngle) {
                                value *= pow(3.2808399, 1);
                            }
                            else if (mode == ByShapeModeTriangle) {
                                CGFloat l1 = [attributesTemp[0] floatValue];
                                CGFloat l2 = [attributesTemp[1] floatValue];
                                CGFloat l3 = [attributesTemp[2] floatValue];
                                CGFloat c = (l1 + l2 + l3) / 2;
                                value = sqrt((c * (c - l1) * (c - l2) * (c - l3)));
                            } else if (mode == ByShapeModeSquare) {
                                CGFloat l1 = [attributesTemp[0] floatValue];
                                CGFloat l2 = [attributesTemp[1] floatValue];
                                value = l1 * l2;
                            } else if (mode == ByShapeModeTrapezoid) {
                                CGFloat l_u = [attributesTemp[0] floatValue];
                                CGFloat l_d = [attributesTemp[1] floatValue];
                                CGFloat h = [attributesTemp[2] floatValue];
                                value = (l_u + l_d) * h / 2;
                            } else {
                                CGFloat l1 = [attributesTemp[0] floatValue];
                                CGFloat l2 = [attributesTemp[1] floatValue];
                                CGFloat l3 = [attributesTemp[2] floatValue];
                                value = l1 * l2 * l3;
                            }
//                            else if (mode != ByShapeModeCuboids) {
//                                value *= pow(3.2808399, 2);
//                            } else {
//                                value *= pow(3.2808399, 3);
//                            }
                        }
                        else if (index == 0 && mode == ByShapeModeAngle) {
                            value = value;
                        }
                        else {
                            value *= 3.2808399;
                        }
                        break;
                    case 2: // 英寸
                        if (index == count - 1) {
                            if (mode == ByShapeModeLine) {
                                value *= pow((3.2808399 * 12), 1);
                            }
                            else if (mode == ByShapeModeAngle) {
                                value *= pow(3.2808399 * 12, 1);
                            }
                            else if (mode == ByShapeModeTriangle) {
                                CGFloat l1 = [attributesTemp[0] floatValue];
                                CGFloat l2 = [attributesTemp[1] floatValue];
                                CGFloat l3 = [attributesTemp[2] floatValue];
                                CGFloat c = (l1 + l2 + l3) / 2;
                                value = sqrt((c * (c - l1) * (c - l2) * (c - l3)));
                            } else if (mode == ByShapeModeSquare) {
                                CGFloat l1 = [attributesTemp[0] floatValue];
                                CGFloat l2 = [attributesTemp[1] floatValue];
                                value = l1 * l2;
                            } else if (mode == ByShapeModeTrapezoid) {
                                CGFloat l_u = [attributesTemp[0] floatValue];
                                CGFloat l_d = [attributesTemp[1] floatValue];
                                CGFloat h = [attributesTemp[2] floatValue];
                                value = (l_u + l_d) * h / 2;
                                
                            } else {
                                CGFloat l1 = [attributesTemp[0] floatValue];
                                CGFloat l2 = [attributesTemp[1] floatValue];
                                CGFloat l3 = [attributesTemp[2] floatValue];
                                value = l1 * l2 * l3;
                            }
//                            else if (mode != ByShapeModeCuboids) {
//                                value *= pow((3.2808399 * 12), 2);
//                            } else {
//                                value *= pow((3.2808399 * 12), 3);
//                            }
                        }
                        else if (index == 0 && mode == ByShapeModeAngle) {
                            value = value;
                        }
                        else {
                            value *= (3.2808399 * 12);
                        }
                        break;
                    case 3: // 英尺+英寸
                        if (index == count - 1) {
                            if (mode == ByShapeModeLine) {
                                value *= pow((3.2808399), 1);
                            }
                            else if (mode == ByShapeModeAngle) {
                                value *= pow(3.2808399, 1);
                            }
                            else if (mode == ByShapeModeTriangle) {
                                CGFloat l1 = [attributesTemp[0] floatValue];
                                CGFloat l2 = [attributesTemp[1] floatValue];
                                CGFloat l3 = [attributesTemp[2] floatValue];
                                CGFloat c = (l1 + l2 + l3) / 2;
                                value = sqrt((c * (c - l1) * (c - l2) * (c - l3)));
                            } else if (mode == ByShapeModeSquare) {
                                CGFloat l1 = [attributesTemp[0] floatValue];
                                CGFloat l2 = [attributesTemp[1] floatValue];
                                value = l1 * l2;
                            } else if (mode == ByShapeModeTrapezoid) {
                                CGFloat l_u = [attributesTemp[0] floatValue];
                                CGFloat l_d = [attributesTemp[1] floatValue];
                                CGFloat h = [attributesTemp[2] floatValue];
                                value = (l_u + l_d) * h / 2;
                                
                            } else {
                                CGFloat l1 = [attributesTemp[0] floatValue];
                                CGFloat l2 = [attributesTemp[1] floatValue];
                                CGFloat l3 = [attributesTemp[2] floatValue];
                                value = l1 * l2 * l3;
                            }
//                            else if (mode != ByShapeModeCuboids) {
//                                value *= pow(3.2808399, 2);
//                            } else {
//                                value *= pow(3.2808399, 3);
//                            }
                        }
                        else if (index == 0 && mode == ByShapeModeAngle) {
                            value = value;
                        }
                        else {
                            value *= 3.2808399;
                        }
                        break;
                    default:
                        break;
                }
                
                
                [attributesTemp addObject:[NSString stringWithFormat:@"%.3lf", value]];

            }
        }
        self.attributes = attributesTemp.mutableCopy;
        
        NSArray *points = dic[@"points"];
        if (mode != ByShapeModeCuboids) {
            [self.points removeAllObjects];
            for (NSDictionary *pointDic in points) {
                CGPoint point = CGPointMake([pointDic[@"x"] floatValue], [pointDic[@"y"] floatValue]);
                [self.points addObject:[NSValue valueWithCGPoint:point]];
            }
//            self.movePoints = [self movePointsFormPoints:self.points];
            self.movePath = [self movePathForNormal];
            [self adjustLabelsFrame];
        } else {
            [self.cuboidsControllPoints removeAllObjects];
            [self.cuboidsDrawUpPoints removeAllObjects];
            [self.cuboidsDrawDownPoints removeAllObjects];
            for (NSDictionary *pointDic in points) {
                NSInteger index = [points indexOfObject:pointDic];
                CGPoint point = CGPointMake([pointDic[@"x"] floatValue], [pointDic[@"y"] floatValue]);
                if (index < 4) {
                    [self.cuboidsDrawUpPoints addObject:[NSValue valueWithCGPoint:point]];
                } else {
                    [self.cuboidsDrawDownPoints addObject:[NSValue valueWithCGPoint:point]];
                }
                
            }
            [self.cuboidsControllPoints addObject:self.cuboidsDrawDownPoints[1]];
            [self.cuboidsControllPoints addObject:self.cuboidsDrawDownPoints[3]];
            [self.cuboidsControllPoints addObject:self.cuboidsDrawUpPoints[2]];
            [self.cuboidsControllPoints addObject:self.cuboidsDrawDownPoints[2]];
//            self.movePoints = [self movePointsForCuboids];
//            self.movePath = [self movePathForCuboids];
            self.movePaths = [self movePathsForCuboids];
            [self adjustLabelsFrame];
        }

    }
    return self;
}

- (id)copyWithZone:(NSZone *)zone {

    ByShapeModel *obj = [[ByShapeModel allocWithZone:zone] init];
    obj.name = self.name;
    obj.picture = self.picture;
    obj.selected = self.selected;
    obj.labels = self.labels;
    obj.mode = self.mode;
    obj.pointView = self.pointView;
    obj.lineView = self.lineView;
    obj.moveView = self.moveView;
    obj.highLightLineView = self.highLightLineView;
    obj.angleView = self.angleView;
    if (self.points) {
        obj.points = [[NSMutableArray alloc] init];
        for (NSValue *pointValue in self.points) {
            [obj.points addObject:pointValue];
        }
//        obj.movePoints = [obj movePointsFormPoints:obj.points];
        obj.movePath = [obj movePathForNormal];
    } else {
        obj.cuboidsControllPoints = [[NSMutableArray alloc] init];
        for (NSValue *pointValue in self.cuboidsControllPoints) {
            [obj.cuboidsControllPoints addObject:pointValue];
        }
        obj.cuboidsDrawUpPoints = [[NSMutableArray alloc] init];
        for (NSValue *pointValue in self.cuboidsDrawUpPoints) {
            [obj.cuboidsDrawUpPoints addObject:pointValue];
        }
        obj.cuboidsDrawDownPoints = [[NSMutableArray alloc] init];
        for (NSValue *pointValue in self.cuboidsDrawDownPoints) {
            [obj.cuboidsDrawDownPoints addObject:pointValue];
        }
        
//        obj.movePoints = [obj movePointsForCuboids];
//        obj.movePath = [obj movePathForCuboids];
        obj.movePaths = [obj movePathsForCuboids];

    }
    obj.attributes = self.attributes;

    
    

    return obj;
}



- (instancetype)initWithMode:(ByShapeMode)mode {
    
    if (self = [super init]) {
        
        self.mode = mode;
        [self initViews];
        [self initPoints];
    }
    
    return self;
}


// 初始化视图
- (void)initViews {
    
    // 线条视图
    _lineView = [[ByShapeLayerView alloc] init];
    _lineView.shapeLayer.lineWidth = 1;
    _lineView.shapeLayer.lineCap = kCALineCapRound;
    _lineView.shapeLayer.lineJoin = kCALineJoinRound;
    _lineView.shapeLayer.fillColor = [UIColor colorWithRed:(16 / 255.0) green:(75 / 255.0) blue:(168 / 255.0) alpha:0.2].CGColor;
    _lineView.shapeLayer.strokeColor = CJ_THEME_COLOR.CGColor;
    _lineView.backgroundColor = [UIColor clearColor];
    _lineView.opaque = NO;
    _lineView.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 编辑触摸点路径
    _pointView = [[ByShapeLayerView alloc] init];
    _pointView.shapeLayer.fillColor = [UIColor yellowColor].CGColor;
    _pointView.backgroundColor = [UIColor clearColor];
    _pointView.opaque = NO;
    _pointView.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 移动点路径
    _moveView = [[ByShapeLayerView alloc] init];
    _moveView.shapeLayer.fillColor = [UIColor orangeColor].CGColor;
    _moveView.backgroundColor = [UIColor clearColor];
    _moveView.backgroundColor = [UIColor clearColor];
    _moveView.opaque = NO;
    _moveView.translatesAutoresizingMaskIntoConstraints = NO;
    
    //
    _highLightLineView = [[ByShapeLayerView alloc] init];
    _highLightLineView.shapeLayer.strokeColor = [UIColor redColor].CGColor;
    _highLightLineView.backgroundColor = [UIColor clearColor];
    _highLightLineView.opaque = NO;
    _highLightLineView.translatesAutoresizingMaskIntoConstraints = NO;
    
    // 三角形有个角度
    if (self.mode == ByShapeModeAngle) {
        _lineView.shapeLayer.fillColor = nil;
        // 角度
        _angleView = [[ByShapeLayerView alloc] init];
        _angleView.shapeLayer.lineWidth = 1;
        _angleView.shapeLayer.lineCap = kCALineCapRound;
        _angleView.shapeLayer.lineJoin = kCALineJoinRound;
        _angleView.shapeLayer.fillColor = nil;
        _angleView.shapeLayer.strokeColor = [UIColor redColor].CGColor;
        _angleView.backgroundColor = [UIColor clearColor];
        _angleView.opaque = NO;
        _angleView.translatesAutoresizingMaskIntoConstraints = NO;
    }
}


// 初始化点
- (void)initPoints {
    
    
    switch (self.mode) {
        case ByShapeModeLine:
        {
            self.points = [NSMutableArray arrayWithArray:@[[NSValue valueWithCGPoint:CGPointMake(100, 100)],
                                                           [NSValue valueWithCGPoint:CGPointMake(200, 100)],]];
//            _movePoints = [self movePointsFormPoints:_points];
            _movePath = [self movePathForNormal];
            [self initAttributeLabels];
            self.name = STRING(@"zhixian");
            break;
        }
        case ByShapeModeAngle:
        {
            self.points = [NSMutableArray arrayWithArray:@[[NSValue valueWithCGPoint:CGPointMake(180, 160)],
                                                           [NSValue valueWithCGPoint:CGPointMake(100, 200)],
                                                           [NSValue valueWithCGPoint:CGPointMake(200, 200)],]];
//            _movePoints = [self movePointsFormPoints:_points];
            _movePath = [self movePathForNormal];
            [self initAttributeLabels];
            self.name = STRING(@"jiaoduxian");
            break;
        }
        case ByShapeModeTriangle:
        {
            self.points = [NSMutableArray arrayWithArray:@[[NSValue valueWithCGPoint:CGPointMake(150, 120)],
                                                           [NSValue valueWithCGPoint:CGPointMake(100, 200)],
                                                           [NSValue valueWithCGPoint:CGPointMake(200, 200)],]];
//            _movePoints = [self movePointsFormPoints:_points];
            _movePath = [self movePathForNormal];
            [self initAttributeLabels];
            self.name = STRING(@"sanjiaoxing");
            break;
        }
        case ByShapeModeSquare:
        {
            self.points = [NSMutableArray arrayWithArray:@[[NSValue valueWithCGPoint:CGPointMake(100, 100)],
                                                       [NSValue valueWithCGPoint:CGPointMake(100, 200)],
                                                       [NSValue valueWithCGPoint:CGPointMake(200, 200)],
                                                       [NSValue valueWithCGPoint:CGPointMake(200, 100)],]];
//            _movePoints = [self movePointsFormPoints:_points];
            _movePath = [self movePathForNormal];
            [self initAttributeLabels];
            self.name = STRING(@"zhengfangxing");
            break;
        }
        case ByShapeModeTrapezoid:
        {
            self.points = [NSMutableArray arrayWithArray:@[[NSValue valueWithCGPoint:CGPointMake(100, 100)],
                                                           [NSValue valueWithCGPoint:CGPointMake(100, 200)],
                                                           [NSValue valueWithCGPoint:CGPointMake(250, 200)],
                                                           [NSValue valueWithCGPoint:CGPointMake(200, 100)],
                                                           ]];
//            _movePoints = [self movePointsFormPoints:_points];
            _movePath = [self movePathForNormal];
            [self initAttributeLabels];
            self.name = STRING(@"tixing");
            break;
        }
        case ByShapeModeCuboids:
        {
            
            self.cuboidsDrawUpPoints = [NSMutableArray arrayWithArray:@[[NSValue valueWithCGPoint:CGPointMake(150, 120)],
                                                                        [NSValue valueWithCGPoint:CGPointMake(100, 150)],
                                                                        [NSValue valueWithCGPoint:CGPointMake(250, 150)],
                                                                        [NSValue valueWithCGPoint:CGPointMake(300, 120)],]];
            self.cuboidsDrawDownPoints = [NSMutableArray arrayWithArray:@[[NSValue valueWithCGPoint:CGPointMake(150, 170)],
                                                                          [NSValue valueWithCGPoint:CGPointMake(100, 200)],
                                                                          [NSValue valueWithCGPoint:CGPointMake(250, 200)],
                                                                          [NSValue valueWithCGPoint:CGPointMake(300, 170)],]];
            self.cuboidsControllPoints = [NSMutableArray arrayWithCapacity:4];
            [self.cuboidsControllPoints addObject:self.cuboidsDrawDownPoints[1]];
            [self.cuboidsControllPoints addObject:self.cuboidsDrawDownPoints[3]];
            [self.cuboidsControllPoints addObject:self.cuboidsDrawUpPoints[2]];
            [self.cuboidsControllPoints addObject:self.cuboidsDrawDownPoints[2]];
            
//            self.movePoints = [self movePointsForCuboids];
//            _movePath = [self movePathForCuboids];
            _movePaths = [self movePathsForCuboids];
            [self initAttributeLabels];
            self.name = STRING(@"changfangti");
            break;
        }
        default:
            break;
    }
}


- (NSArray *)movePathsForCuboids {
    
    // 上面
    UIBezierPath *path1 = [[UIBezierPath alloc] init];
    [path1 moveToPoint:[self.cuboidsDrawUpPoints[0] CGPointValue]];
    [path1 addLineToPoint:[self.cuboidsDrawUpPoints[1] CGPointValue]];
    [path1 addLineToPoint:[self.cuboidsDrawUpPoints[2] CGPointValue]];
    [path1 addLineToPoint:[self.cuboidsDrawUpPoints[3] CGPointValue]];
    [path1 closePath];

    // 下面
    UIBezierPath *path2 = [[UIBezierPath alloc] init];
    [path2 moveToPoint:[self.cuboidsDrawDownPoints[0] CGPointValue]];
    [path2 addLineToPoint:[self.cuboidsDrawDownPoints[1] CGPointValue]];
    [path2 addLineToPoint:[self.cuboidsDrawDownPoints[2] CGPointValue]];
    [path2 addLineToPoint:[self.cuboidsDrawDownPoints[3] CGPointValue]];
    [path2 closePath];

    // 前面
    UIBezierPath *path3 = [[UIBezierPath alloc] init];
    [path3 moveToPoint:[self.cuboidsDrawUpPoints[1] CGPointValue]];
    [path3 addLineToPoint:[self.cuboidsDrawDownPoints[1] CGPointValue]];
    [path3 addLineToPoint:[self.cuboidsDrawDownPoints[2] CGPointValue]];
    [path3 addLineToPoint:[self.cuboidsDrawUpPoints[2] CGPointValue]];
    [path3 closePath];

    // 后面
    UIBezierPath *path4 = [[UIBezierPath alloc] init];
    [path4 moveToPoint:[self.cuboidsDrawUpPoints[0] CGPointValue]];
    [path4 addLineToPoint:[self.cuboidsDrawDownPoints[0] CGPointValue]];
    [path4 addLineToPoint:[self.cuboidsDrawDownPoints[3] CGPointValue]];
    [path4 addLineToPoint:[self.cuboidsDrawUpPoints[3] CGPointValue]];
    [path4 closePath];

    // 左面
    UIBezierPath *path5 = [[UIBezierPath alloc] init];
    [path5 moveToPoint:[self.cuboidsDrawUpPoints[0] CGPointValue]];
    [path5 addLineToPoint:[self.cuboidsDrawUpPoints[1] CGPointValue]];
    [path5 addLineToPoint:[self.cuboidsDrawDownPoints[1] CGPointValue]];
    [path5 addLineToPoint:[self.cuboidsDrawDownPoints[0] CGPointValue]];
    [path5 closePath];

    // 右面
    UIBezierPath *path6 = [[UIBezierPath alloc] init];
    [path6 moveToPoint:[self.cuboidsDrawUpPoints[3] CGPointValue]];
    [path6 addLineToPoint:[self.cuboidsDrawUpPoints[2] CGPointValue]];
    [path6 addLineToPoint:[self.cuboidsDrawDownPoints[2] CGPointValue]];
    [path6 addLineToPoint:[self.cuboidsDrawDownPoints[3] CGPointValue]];
    [path6 closePath];

    return @[path1, path2, path3, path4, path5, path6];
}
//- (NSArray *)movePointsForCuboids {
//    
//    NSMutableArray *movePoints = [[NSMutableArray alloc] init];
//    
//    // 上面
//    [self.cuboidsDrawUpPoints enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        NSValue *pointValue = obj;
//        CGPoint point = pointValue.CGPointValue;
//        // 不是最后一个点
//        if (idx != self.cuboidsDrawUpPoints.count - 1) {
//            CGPoint nextPoint = [self.cuboidsDrawUpPoints[idx + 1] CGPointValue];
//            NSArray *points = [self pointsToMoveFormPoint1:point point2:nextPoint];
//            [movePoints addObjectsFromArray:points];
//        } else {
//            CGPoint firstPoint = [self.cuboidsDrawUpPoints[0] CGPointValue];
//            NSArray *points = [self pointsToMoveFormPoint1:firstPoint point2:point];
//            [movePoints addObjectsFromArray:points];
//        }
//    }];
//    
//    // 下面
//    [self.cuboidsDrawDownPoints enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        NSValue *pointValue = obj;
//        CGPoint point = pointValue.CGPointValue;
//        // 不是最后一个点
//        if (idx != self.cuboidsDrawDownPoints.count - 1) {
//            CGPoint nextPoint = [self.cuboidsDrawDownPoints[idx + 1] CGPointValue];
//            NSArray *points = [self pointsToMoveFormPoint1:point point2:nextPoint];
//            [movePoints addObjectsFromArray:points];
//        } else {
//            CGPoint firstPoint = [self.cuboidsDrawDownPoints[0] CGPointValue];
//            NSArray *points = [self pointsToMoveFormPoint1:firstPoint point2:point];
//            [movePoints addObjectsFromArray:points];
//        }
//    }];
//    
//    // 四条棱
//    NSArray *points1 = [self pointsToMoveFormPoint1:[self.cuboidsDrawUpPoints[0] CGPointValue]
//                                            point2:[self.cuboidsDrawDownPoints[0] CGPointValue]];
//    NSArray *points2 = [self pointsToMoveFormPoint1:[self.cuboidsDrawUpPoints[1] CGPointValue]
//                                            point2:[self.cuboidsDrawDownPoints[1] CGPointValue]];
//    NSArray *points3 = [self pointsToMoveFormPoint1:[self.cuboidsDrawUpPoints[2] CGPointValue]
//                                            point2:[self.cuboidsDrawDownPoints[2] CGPointValue]];
//    NSArray *points4 = [self pointsToMoveFormPoint1:[self.cuboidsDrawUpPoints[3] CGPointValue]
//                                            point2:[self.cuboidsDrawDownPoints[3] CGPointValue]];
//    [movePoints addObjectsFromArray:points1];
//    [movePoints addObjectsFromArray:points2];
//    [movePoints addObjectsFromArray:points3];
//    [movePoints addObjectsFromArray:points4];
//
//    return [movePoints copy];
//}

- (UIBezierPath *)movePathForNormal {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    if (self.mode != ByShapeModeLine) {
        [path moveToPoint:[self.points[0] CGPointValue]];
        for (int i = 1; i < self.points.count ; i ++) {
            [path addLineToPoint:[self.points[i] CGPointValue]];
        }
        [path closePath];
    }
    else {
        CGPoint bp1 = [self.points[0] CGPointValue];
        CGPoint bp2 = [self.points[1] CGPointValue];

        CGPoint p1 = CGPointMake(bp1.x, bp1.y - 20);
        CGPoint p2 = CGPointMake(bp1.x, bp1.y + 20);
        CGPoint p3 = CGPointMake(bp2.x, bp2.y + 20);
        CGPoint p4 = CGPointMake(bp2.x, bp2.y - 20);
    
        [path moveToPoint:p1];
        [path addLineToPoint:p2];
        [path addLineToPoint:p3];
        [path addLineToPoint:p4];

        [path closePath];
    }
    return path;
}
//- (NSArray *)movePointsFormPoints:(NSArray *)points {
//
//    NSMutableArray *movePoints = [[NSMutableArray alloc] init];
//
//    [points enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        NSValue *pointValue = obj;
//        CGPoint point = pointValue.CGPointValue;
//        // 不是最后一个点
//        if (idx != points.count - 1) {
//            CGPoint nextPoint = [points[idx + 1] CGPointValue];
//            NSArray *points = [self pointsToMoveFormPoint1:point point2:nextPoint];
//            [movePoints addObjectsFromArray:points];
//        } else {
//            CGPoint firstPoint = [points[0] CGPointValue];
//            NSArray *points = [self pointsToMoveFormPoint1:firstPoint point2:point];
//            [movePoints addObjectsFromArray:points];
//        }
//    }];
//
//    return [movePoints copy];
//}

static CGFloat const kMovePointDiameter = 20;              // 拖拽点的直径
- (NSArray *)pointsToMoveFormPoint1:(CGPoint)point1 point2:(CGPoint)point2 {

    NSMutableArray *movePoints = [[NSMutableArray alloc] init];

    CGFloat deltX = ABS(point1.x - point2.x);
    CGFloat deltY = ABS(point1.y - point2.y);
    CGFloat length = sqrt(deltX * deltX + deltY * deltY);
    NSInteger count = (NSInteger)floor(length / kMovePointDiameter);
    for (int i = 1; i < count; i ++) {
        CGPoint point = CGPointZero;
        if (point1.x >= point2.x && point1.y >= point2.y) {
            point.x = point2.x + (point1.x - point2.x) / count * i;
            point.y = point2.y + (point1.y - point2.y) / count * i;
        }
        else if (point1.x >= point2.x && point1.y <= point2.y) {
            point.x = point2.x + (point1.x - point2.x) / count * i;
            point.y = point2.y - (point2.y - point1.y) / count * i;
        }
        else if (point1.x <= point2.x && point1.y >= point2.y) {
            point.x = point1.x + (point2.x - point1.x) / count * i;
            point.y = point1.y - (point1.y - point2.y) / count * i;
        }
        else if (point1.x <= point2.x && point1.y <= point2.y) {
            point.x = point1.x + (point2.x - point1.x) / count * i;
            point.y = point1.y + (point2.y - point1.y) / count * i;
        }
        [movePoints addObject:[NSValue valueWithCGPoint:point]];
    }
    return [movePoints copy];
}


#pragma mark - 废弃（获得移动点，但只有两点之间的一点）
- (NSArray *)pointsCanMoveWithPoints:(NSArray *)points {
    
    NSMutableArray *movePoints = [[NSMutableArray alloc] init];
    
    [_points enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSValue *pointValue = obj;
        CGPoint point = pointValue.CGPointValue;
        // 不是最后一个点
        if (idx != _points.count - 1) {
            CGPoint nextPoint = [_points[idx + 1] CGPointValue];
            CGPoint movePoint = [self pointFromPoint1:point point2:nextPoint];
            [movePoints addObject:[NSValue valueWithCGPoint:movePoint]];
        } else {
            CGPoint firstPoint = [_points[0] CGPointValue];
            CGPoint movePoint = [self pointFromPoint1:firstPoint point2:point];
            [movePoints addObject:[NSValue valueWithCGPoint:movePoint]];
        }
    }];
    
    return [movePoints copy];
}
// 根据两点和边长来获得一个rect
- (CGPoint)pointFromPoint1:(CGPoint)point1 point2:(CGPoint)point2{
    
    CGFloat x, y;
    x = (point1.x + point2.x) / 2;
    y = (point1.y + point2.y) / 2;
    CGPoint point = CGPointMake(x, y);
    
    return point;
}



#pragma mark - 属性label相关
- (void)setHighLightLabel:(UILabel *)highLightLabel distance:(NSString *)distance {
    
    [self setHighLightLabel:highLightLabel];
    
    if (distance == nil) {
        return;
    }
    
    NSInteger index = [_labels indexOfObject:highLightLabel];
    NSMutableArray *attribrtes = self.attributes;

    switch (self.mode) {
        case ByShapeModeLine:{
            
            [attribrtes replaceObjectAtIndex:0 withObject:distance];
            self.attributes = attribrtes;
            break;
        }
        case ByShapeModeAngle:{
            switch (index) {
                case 0:
                    break;
                case 1:
                    [attribrtes replaceObjectAtIndex:1 withObject:distance];
                    break;
                case 2:
                    [attribrtes replaceObjectAtIndex:2 withObject:distance];
                    break;
                default:
                    break;
            }
            self.attributes = attribrtes;
            break;
        }
        case ByShapeModeTriangle:{
            switch (index) {
                case 0:
                    [attribrtes replaceObjectAtIndex:0 withObject:distance];
                    break;
                case 1:
                    [attribrtes replaceObjectAtIndex:1 withObject:distance];
                    break;
                case 2:
                    [attribrtes replaceObjectAtIndex:2 withObject:distance];
                    break;
                default:
                    break;
            }
            if ([attribrtes[0] floatValue] != 0 && [attribrtes[1] floatValue] != 0 && [attribrtes[2] floatValue] != 0) {
                CGFloat l1 = [attribrtes[0] floatValue];
                CGFloat l2 = [attribrtes[1] floatValue];
                CGFloat l3 = [attribrtes[2] floatValue];
                // 计算面积
                CGFloat c = (l1 + l2 + l3) / 2;
                CGFloat s = sqrt((c * (c - l1) * (c - l2) * (c - l3)));
                if (isnan(s) || s == 0) {
                    [UIAlertView showWithTitle:nil message:STRING(@"bianchangshezhibuzhengque") cancelButtonTitle:STRING(@"queding") otherButtonTitles:nil tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                        [attribrtes replaceObjectAtIndex:0 withObject:@"L1"];
                        [attribrtes replaceObjectAtIndex:1 withObject:@"L2"];
                        [attribrtes replaceObjectAtIndex:2 withObject:@"L3"];
                        [attribrtes replaceObjectAtIndex:3 withObject:@"S"];
                        self.attributes = attribrtes;

                    }];

                } else {
                    [attribrtes replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"%.3lf", s]];
                }
            }
            self.attributes = attribrtes;
            break;
        }
        case ByShapeModeSquare:{
            switch (index) {
                case 0:
                    [attribrtes replaceObjectAtIndex:0 withObject:distance];
                    break;
                case 1:
                    [attribrtes replaceObjectAtIndex:1 withObject:distance];
                    break;
                default:
                    break;
            }
            if ([attribrtes[0] floatValue] != 0 && [attribrtes[1] floatValue] != 0) {
                CGFloat l1 = [attribrtes[0] floatValue];
                CGFloat l2 = [attribrtes[1] floatValue];
                // 计算面积
                CGFloat s = l1 * l2;
                [attribrtes replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"%.3lf", s]];
            }
            self.attributes = attribrtes;
            break;
        }
        case ByShapeModeTrapezoid:{
            switch (index) {
                case 0:
                    [attribrtes replaceObjectAtIndex:0 withObject:distance];
                    break;
                case 1:
                    [attribrtes replaceObjectAtIndex:1 withObject:distance];
                    break;
                case 2:
                    [attribrtes replaceObjectAtIndex:2 withObject:distance];
                    break;
                default:
                    break;
            }
            if ([attribrtes[0] floatValue] != 0 && [attribrtes[1] floatValue] != 0 && [attribrtes[2] floatValue] != 0) {
                
                CGFloat l1 = [attribrtes[0] floatValue];
                CGFloat l2 = [attribrtes[1] floatValue];
                CGFloat h = [attribrtes[2] floatValue];

                // 计算面积
                CGFloat s = (l1 + l2) * h / 2;
                [attribrtes replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"%.3lf", s]];
            }
            self.attributes = attribrtes;
            break;
        }
        case ByShapeModeCuboids:{
            switch (index) {
                case 0:
                    [attribrtes replaceObjectAtIndex:0 withObject:distance];
                    break;
                case 1:
                    [attribrtes replaceObjectAtIndex:1 withObject:distance];
                    break;
                case 2:
                    [attribrtes replaceObjectAtIndex:2 withObject:distance];
                    break;
                default:
                    break;
            }
            if ([attribrtes[0] floatValue] != 0 && [attribrtes[1] floatValue] != 0 && [attribrtes[2] floatValue] != 0) {
                
                CGFloat l1 = [attribrtes[0] floatValue];
                CGFloat l2 = [attribrtes[1] floatValue];
                CGFloat l3 = [attribrtes[2] floatValue];
                
                // 计算面积
                CGFloat s = l1 * l2 * l3;
                [attribrtes replaceObjectAtIndex:3 withObject:[NSString stringWithFormat:@"%.3lf", s]];
            }
            self.attributes = attribrtes;
            break;
        }
            
        default:
            break;
    }

    
}

- (void)setHighLightLabel:(UILabel *)highLightLabel {
    
    _highLightLabel = highLightLabel;
    
    if (_highLightLabel == nil) {
        _highLightPoints = nil;
        return;
    }
    
    _highLightPoints = [[NSMutableArray alloc] init];
    
    NSInteger index = [_labels indexOfObject:_highLightLabel];
    switch (self.mode) {
        case ByShapeModeLine:{
            
            [_highLightPoints addObject:_points[0]];
            [_highLightPoints addObject:_points[1]];
            break;
        }
        case ByShapeModeAngle:{
            switch (index) {
                case 0:
                    _highLightPoints = nil;
                    break;
                case 1:
                    [_highLightPoints addObject:_points[0]];
                    [_highLightPoints addObject:_points[1]];
                    break;
                case 2:
                    [_highLightPoints addObject:_points[1]];
                    [_highLightPoints addObject:_points[2]];
                    break;
                default:
                    _highLightPoints = nil;
                    break;
            }
            break;
        }
        case ByShapeModeTriangle:{
            switch (index) {
                case 0:
                    [_highLightPoints addObject:_points[0]];
                    [_highLightPoints addObject:_points[1]];
                    break;
                case 1:
                    [_highLightPoints addObject:_points[1]];
                    [_highLightPoints addObject:_points[2]];
                    break;
                case 2:
                    [_highLightPoints addObject:_points[2]];
                    [_highLightPoints addObject:_points[0]];
                    break;
                default:
                    _highLightPoints = nil;
                    break;
            }
            
            break;
        }
        case ByShapeModeSquare:{
            switch (index) {
                case 0:
                    [_highLightPoints addObject:_points[1]];
                    [_highLightPoints addObject:_points[2]];
                    break;
                case 1:
                    [_highLightPoints addObject:_points[2]];
                    [_highLightPoints addObject:_points[3]];
                    break;
                default:
                    _highLightPoints = nil;
                    break;
            }
            break;
        }
        case ByShapeModeTrapezoid:{
            switch (index) {
                case 0:
                    [_highLightPoints addObject:_points[0]];
                    [_highLightPoints addObject:_points[3]];
                    break;
                case 1:
                    [_highLightPoints addObject:_points[1]];
                    [_highLightPoints addObject:_points[2]];
                    break;
                case 2:
                    [_highLightPoints addObject:_points[0]];
                    [_highLightPoints addObject:_points[1]];
                    break;
                default:
                    _highLightPoints = nil;
                    break;
            }
            break;
        }
        case ByShapeModeCuboids:{
            switch (index) {
                case 0:
                    [_highLightPoints addObject:_cuboidsDrawDownPoints[1]];
                    [_highLightPoints addObject:_cuboidsDrawDownPoints[2]];
                    break;
                case 1:
                    [_highLightPoints addObject:_cuboidsDrawDownPoints[2]];
                    [_highLightPoints addObject:_cuboidsDrawDownPoints[3]];
                    break;
                case 2:
                    [_highLightPoints addObject:_cuboidsDrawDownPoints[2]];
                    [_highLightPoints addObject:_cuboidsDrawUpPoints[2]];
                    break;
                default:
                    _highLightPoints = nil;
                    break;
            }
            break;
        }
            
        default:
            break;
    }

}

// type：0，一次；1：二次；2：三次；3：角度
- (void)setAttributeLabelUnit:(UILabel *)label type:(int)type {
    
    if (type == 3) {
        label.text = [NSString stringWithFormat:@"%@ °", label.text];
        return;
    } else {
        int unit = [ByCommonCache getSystemUnit].intValue;
        switch (unit) {
            case 0: // 米
                if (type == 0) {
                    label.text = [NSString stringWithFormat:@"%@ m", label.text];
                }
                if (type == 1) {
                    label.text = [NSString stringWithFormat:@"%@ m²", label.text];
                }
                if (type == 2) {
                    label.text = [NSString stringWithFormat:@"%@ m³", label.text];
                }
                break;
            case 1: // 英尺
                if (type == 0) {
                    label.text = [NSString stringWithFormat:@"%@ ft", label.text];
                }
                if (type == 1) {
                    label.text = [NSString stringWithFormat:@"%@ ft²", label.text];
                }
                if (type == 2) {
                    label.text = [NSString stringWithFormat:@"%@ ft³", label.text];
                }
                break;
            case 2: // 英寸
                if (type == 0) {
                    label.text = [NSString stringWithFormat:@"%@ in", label.text];
                }
                if (type == 1) {
                    label.text = [NSString stringWithFormat:@"%@ in²", label.text];
                }
                if (type == 2) {
                    label.text = [NSString stringWithFormat:@"%@ in³", label.text];
                }
                break;
            case 3: // 英尺+英寸
                if (type == 0) {
                    label.text = [NSString stringWithFormat:@"%@ ft", label.text];
                }
                if (type == 1) {
                    label.text = [NSString stringWithFormat:@"%@ ft²", label.text];
                }
                if (type == 2) {
                    label.text = [NSString stringWithFormat:@"%@ ft³", label.text];
                }
                break;
            default:
                break;
        }

    }
   
}

- (void)setAttributes:(NSMutableArray *)attributes {
    
    _attributes = attributes;
    
    switch (self.mode) {
        case ByShapeModeLine:{
            ByShapeLayerView *label = [_labels firstObject];
            label.text = _attributes[0];
            [self setAttributeLabelUnit:label type:0];
            label.width = MAX([label.text getSizeOfString:10].width + 5, 15);
            break;
        }
        case ByShapeModeAngle:{
            ByShapeLayerView *label1 = [_labels objectAtIndex:0];
            ByShapeLayerView *label2 = [_labels objectAtIndex:1];
            ByShapeLayerView *label3 = [_labels objectAtIndex:2];
            label1.text = _attributes[0];
            [self setAttributeLabelUnit:label1 type:3];
            label2.text = _attributes[1];
            [self setAttributeLabelUnit:label2 type:0];
            label3.text = _attributes[2];
            [self setAttributeLabelUnit:label3 type:0];
            label1.width = MAX([label1.text getSizeOfString:10].width + 5, 15);
            label2.width = MAX([label2.text getSizeOfString:10].width + 5, 15);
            label3.width = MAX([label3.text getSizeOfString:10].width + 5, 15);

            break;
        }
        case ByShapeModeTriangle:{
            ByShapeLayerView *label1 = [_labels objectAtIndex:0];
            ByShapeLayerView *label2 = [_labels objectAtIndex:1];
            ByShapeLayerView *label3 = [_labels objectAtIndex:2];
            ByShapeLayerView *labelS = [_labels objectAtIndex:3];
            label1.text = _attributes[0];
            [self setAttributeLabelUnit:label1 type:0];
            label2.text = _attributes[1];
            [self setAttributeLabelUnit:label2 type:0];
            label3.text = _attributes[2];
            [self setAttributeLabelUnit:label3 type:0];
            labelS.text = _attributes[3];
            [self setAttributeLabelUnit:labelS type:1];
            label1.width = MAX([label1.text getSizeOfString:10].width + 5, 15);
            label2.width = MAX([label2.text getSizeOfString:10].width + 5, 15);
            label3.width = MAX([label3.text getSizeOfString:10].width + 5, 15);
            labelS.width = MAX([labelS.text getSizeOfString:10].width + 5, 15);

            break;
        }
        case ByShapeModeSquare:{
            ByShapeLayerView *label1 = [_labels objectAtIndex:0];
            ByShapeLayerView *label2 = [_labels objectAtIndex:1];
            ByShapeLayerView *labelS = [_labels objectAtIndex:2];
            label1.text = _attributes[0];
            [self setAttributeLabelUnit:label1 type:0];
            label2.text = _attributes[1];
            [self setAttributeLabelUnit:label2 type:0];
            labelS.text = _attributes[2];
            [self setAttributeLabelUnit:labelS type:1];
            label1.width = MAX([label1.text getSizeOfString:10].width + 5, 15);
            label2.width = MAX([label2.text getSizeOfString:10].width + 5, 15);
            labelS.width = MAX([labelS.text getSizeOfString:10].width + 5, 15);
            break;
        }
        case ByShapeModeTrapezoid:{
            ByShapeLayerView *labelUp = [_labels objectAtIndex:0];
            ByShapeLayerView *labelDown = [_labels objectAtIndex:1];
            ByShapeLayerView *labelH = [_labels objectAtIndex:2];
            ByShapeLayerView *labelS = [_labels objectAtIndex:3];
            labelUp.text = _attributes[0];
            [self setAttributeLabelUnit:labelUp type:0];
            labelDown.text = _attributes[1];
            [self setAttributeLabelUnit:labelDown type:0];
            labelH.text = _attributes[2];
            [self setAttributeLabelUnit:labelH type:0];
            labelS.text = _attributes[3];
            [self setAttributeLabelUnit:labelS type:1];
            labelUp.width = MAX([labelUp.text getSizeOfString:10].width + 5, 15);
            labelDown.width = MAX([labelDown.text getSizeOfString:10].width + 5, 15);
            labelH.width = MAX([labelH.text getSizeOfString:10].width + 5, 15);
            labelS.width = MAX([labelS.text getSizeOfString:10].width + 5, 15);
            break;
        }
        case ByShapeModeCuboids:{
            ByShapeLayerView *labelL = [_labels objectAtIndex:0];
            ByShapeLayerView *labelW = [_labels objectAtIndex:1];
            ByShapeLayerView *labelH = [_labels objectAtIndex:2];
            ByShapeLayerView *labelV = [_labels objectAtIndex:3];
            labelL.text = _attributes[0];
            [self setAttributeLabelUnit:labelL type:0];
            labelW.text = _attributes[1];
            [self setAttributeLabelUnit:labelW type:0];
            labelH.text = _attributes[2];
            [self setAttributeLabelUnit:labelH type:0];
            labelV.text = _attributes[3];
            [self setAttributeLabelUnit:labelV type:2];
            labelL.width = MAX([labelL.text getSizeOfString:10].width + 5, 15);
            labelW.width = MAX([labelW.text getSizeOfString:10].width + 5, 15);
            labelH.width = MAX([labelH.text getSizeOfString:10].width + 5, 15);
            labelV.width = MAX([labelV.text getSizeOfString:10].width + 5, 15);
            break;
        }
            
        default:
            break;
    }
    
    [self adjustLabelsFrame];

}
- (void)adjustLabelsFrame {
    
    switch (self.mode) {
        case ByShapeModeLine:{
            ByShapeLayerView *label = [_labels firstObject];
            [self adjustLabelFrame:label
                            point1:self.points[0]
                            point2:self.points[1]];
            break;
        }
        case ByShapeModeAngle:{
            ByShapeLayerView *label1 = [_labels objectAtIndex:0];
            [self adjustAngleLabelFrame:label1
                                  point:self.points[1]
                                 point1:self.points[0]
                                 point2:self.points[2]];
            ByShapeLayerView *label2 = [_labels objectAtIndex:1];
            [self adjustLabelFrame:label2
                            point1:self.points[0]
                            point2:self.points[1]];
            ByShapeLayerView *label3 = [_labels objectAtIndex:2];
            [self adjustLabelFrame:label3
                            point1:self.points[1]
                            point2:self.points[2]];

            break;
        }
        case ByShapeModeTriangle:{
            ByShapeLayerView *label1 = [_labels objectAtIndex:0];
            [self adjustLabelFrame:label1
                            point1:self.points[0]
                            point2:self.points[1]];
            ByShapeLayerView *label2 = [_labels objectAtIndex:1];
            [self adjustLabelFrame:label2
                            point1:self.points[1]
                            point2:self.points[2]];
            ByShapeLayerView *label3 = [_labels objectAtIndex:2];
            [self adjustLabelFrame:label3
                            point1:self.points[2]
                            point2:self.points[0]];
            CGPoint point1 = [self.points[1] CGPointValue];
            CGPoint point2 = [self.points[2] CGPointValue];
            CGPoint center = CGPointMake((point1.x + point2.x) / 2, (point1.y + point2.y) / 2);
            ByShapeLayerView *labelS = [_labels objectAtIndex:3];
            [self adjustLabelFrame:labelS
                            point1:self.points[0]
                            point2:[NSValue valueWithCGPoint:center]];
            break;
        }
        case ByShapeModeSquare:{
            ByShapeLayerView *label1 = [_labels objectAtIndex:0];
            [self adjustLabelFrame:label1
                            point1:self.points[1]
                            point2:self.points[2]];
            ByShapeLayerView *label2 = [_labels objectAtIndex:1];
            [self adjustLabelFrame:label2
                            point1:self.points[2]
                            point2:self.points[3]];
            ByShapeLayerView *labelS = [_labels objectAtIndex:2];
            [self adjustAreaLabelFrame:labelS];
            break;
        }
        case ByShapeModeTrapezoid:{
            ByShapeLayerView *labelUp = [_labels objectAtIndex:0];
            [self adjustLabelFrame:labelUp
                            point1:self.points[0]
                            point2:self.points[3]];
            ByShapeLayerView *labelDown = [_labels objectAtIndex:1];
            [self adjustLabelFrame:labelDown
                            point1:self.points[1]
                            point2:self.points[2]];
            ByShapeLayerView *labelH = [_labels objectAtIndex:2];
            [self adjustLabelFrame:labelH
                            point1:self.points[0]
                            point2:self.points[1]];
            ByShapeLayerView *labelS = [_labels objectAtIndex:3];
            [self adjustAreaLabelFrame:labelS];

            break;
        }
        case ByShapeModeCuboids:{
            ByShapeLayerView *labelL = [_labels objectAtIndex:0];
            [self adjustLabelFrame:labelL
                            point1:self.cuboidsDrawDownPoints[2]
                            point2:self.cuboidsDrawDownPoints[1]];
            ByShapeLayerView *labelW = [_labels objectAtIndex:1];
            [self adjustLabelFrame:labelW
                            point1:self.cuboidsDrawDownPoints[2]
                            point2:self.cuboidsDrawDownPoints[3]];
            ByShapeLayerView *labelH = [_labels objectAtIndex:2];
            [self adjustLabelFrame:labelH
                            point1:self.cuboidsDrawDownPoints[2]
                            point2:self.cuboidsDrawUpPoints[2]];
            ByShapeLayerView *labelV = [_labels objectAtIndex:3];
            [self adjustLabelFrame:labelV
                            point1:self.cuboidsDrawDownPoints[2]
                            point2:self.cuboidsDrawUpPoints[0]];
            break;
        }
            
        default:
            break;
    }
}
// 生成属性label
- (void)initAttributeLabels {
    
    _labels = [[NSMutableArray alloc] init];
    _attributes = [[NSMutableArray alloc] init];
    switch (self.mode) {
        case ByShapeModeLine:{
            ByShapeLayerView *label = [self createLabelWithPoint:self.points[0]
                                                          point2:self.points[1]
                                                            text:@"L"];
            [_labels addObject:label];
            [_attributes addObject:@"L"];
            break;
        }
        case ByShapeModeAngle:{
            ByShapeLayerView *label1 = [self createLabelWithPoint:self.points[0]
                                                          point2:self.points[2]
                                                            text:@"α"];
            ByShapeLayerView *label2 = [self createLabelWithPoint:self.points[0]
                                                          point2:self.points[1]
                                                            text:@"L1"];
            ByShapeLayerView *label3 = [self createLabelWithPoint:self.points[1]
                                                          point2:self.points[2]
                                                            text:@"L2"];

            [_labels addObject:label1];
            [_labels addObject:label2];
            [_labels addObject:label3];

            [_attributes addObject:@"α"];
            [_attributes addObject:@"L1"];
            [_attributes addObject:@"L2"];

            break;
        }
        case ByShapeModeTriangle:{
            ByShapeLayerView *label1 = [self createLabelWithPoint:self.points[0]
                                                           point2:self.points[1]
                                                             text:@"L1"];
            ByShapeLayerView *label2 = [self createLabelWithPoint:self.points[1]
                                                           point2:self.points[2]
                                                             text:@"L2"];
            ByShapeLayerView *label3 = [self createLabelWithPoint:self.points[2]
                                                           point2:self.points[0]
                                                             text:@"L3"];
            CGPoint point1 = [self.points[1] CGPointValue];
            CGPoint point2 = [self.points[2] CGPointValue];
            CGPoint center = CGPointMake((point1.x + point2.x) / 2, (point1.y + point2.y) / 2);
            ByShapeLayerView *labelS = [self createLabelWithPoint:self.points[0]
                                                           point2:[NSValue valueWithCGPoint:center]
                                                             text:@"S"];
            [_labels addObject:label1];
            [_labels addObject:label2];
            [_labels addObject:label3];
            [_labels addObject:labelS];
            [_attributes addObject:@"L1"];
            [_attributes addObject:@"L2"];
            [_attributes addObject:@"L3"];
            [_attributes addObject:@"S"];

            break;
        }
        case ByShapeModeSquare:{
            ByShapeLayerView *label1 = [self createLabelWithPoint:self.points[1]
                                                          point2:self.points[2]
                                                            text:@"L"];
            ByShapeLayerView *label2 = [self createLabelWithPoint:self.points[2]
                                                          point2:self.points[3]
                                                            text:@"W"];
            // 面积
            ByShapeLayerView *labelS = [self createLabelWithPoint:self.points[0]
                                                           point2:self.points[2]
                                                             text:@"S"];
            [_labels addObject:label1];
            [_labels addObject:label2];
            [_labels addObject:labelS];
            [_attributes addObject:@"L"];
            [_attributes addObject:@"W"];
            [_attributes addObject:@"S"];
            

            break;
        }
        case ByShapeModeTrapezoid:{

            ByShapeLayerView *labelUp = [self createLabelWithPoint:self.points[0]
                                                           point2:self.points[3]
                                                             text:@"L1"];
            ByShapeLayerView *labelDown = [self createLabelWithPoint:self.points[1]
                                                           point2:self.points[2]
                                                             text:@"L2"];
            ByShapeLayerView *labelH = [self createLabelWithPoint:self.points[0]
                                                           point2:self.points[1]
                                                             text:@"H"];
            ByShapeLayerView *labelS = [self createLabelWithPoint:self.points[0]
                                                           point2:self.points[2]
                                                             text:@"S"];
            [_labels addObject:labelUp];
            [_labels addObject:labelDown];
            [_labels addObject:labelH];
            [_labels addObject:labelS];
            [_attributes addObject:@"L1"];
            [_attributes addObject:@"L2"];
            [_attributes addObject:@"H"];
            [_attributes addObject:@"S"];
            

            break;
        }
        case ByShapeModeCuboids:{
            ByShapeLayerView *labelL = [self createLabelWithPoint:self. cuboidsDrawDownPoints[2]
                                                            point2:self.cuboidsDrawDownPoints[1]
                                                              text:@"L"];
            ByShapeLayerView *labelW = [self createLabelWithPoint:self.cuboidsDrawDownPoints[2]
                                                              point2:self.cuboidsDrawDownPoints[3]
                                                                text:@"W"];
            ByShapeLayerView *labelH = [self createLabelWithPoint:self.cuboidsDrawDownPoints[2]
                                                           point2:self.cuboidsDrawUpPoints[2]
                                                             text:@"H"];
            ByShapeLayerView *labelV = [self createLabelWithPoint:self.cuboidsDrawDownPoints[2]
                                                           point2:self.cuboidsDrawUpPoints[0]
                                                             text:@"V"];
            [_labels addObject:labelL];
            [_labels addObject:labelW];
            [_labels addObject:labelH];
            [_labels addObject:labelV];
            [_attributes addObject:@"L"];
            [_attributes addObject:@"W"];
            [_attributes addObject:@"H"];
            [_attributes addObject:@"V"];
            
            break;
        }
            
        default:
            break;
    }
    
    [self setAttributes:_attributes];
    
}

- (ByShapeLayerView *)createLabelWithPoint:(NSValue *)pointValue1 point2:(NSValue *)pointValue2 text:(NSString *)text {

    CGFloat width = [text getSizeOfString:10].width + 5;
    width = MAX(width, 15);
    ByShapeLayerView *label = [[ByShapeLayerView alloc] initWithFrame:CGRectMake(0, 0, width, 15)];
    label.backgroundColor = [UIColor purpleColor];
    label.font = FONT(10);
    label.text = text;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.layer.cornerRadius = 7.5;
    label.layer.masksToBounds = YES;
    
    CGPoint point1 = pointValue1.CGPointValue;
    CGPoint point2 = pointValue2.CGPointValue;
    
    label.x = (point1.x + point2.x) / 2;
    label.y = (point1.y + point2.y) / 2;
    
    return label;
}

// 取两点中间作为调整点
- (void)adjustLabelFrame:(UILabel *)label point1:(NSValue *)pointValue1 point2:(NSValue *)pointValue2 {
    
    CGPoint point1 = pointValue1.CGPointValue;
    CGPoint point2 = pointValue2.CGPointValue;
    
    label.x = (point1.x + point2.x) / 2;
    label.y = (point1.y + point2.y) / 2;
}
- (void)adjustAreaLabelFrame:(UILabel *)label {
    
    CGPoint point0 = [self.points[0] CGPointValue];
    CGPoint point1 = [self.points[1] CGPointValue];
    CGPoint point2 = [self.points[2] CGPointValue];
    CGPoint point3 = [self.points[3] CGPointValue];
    
    CGPoint centerPoint03 = CGPointMake((point0.x + point3.x) / 2, (point0.y + point3.y) / 2);
    CGPoint centerPoint12 = CGPointMake((point1.x + point2.x) / 2, (point1.y + point2.y) / 2);
    CGPoint centerPoint03_12 = CGPointMake((centerPoint03.x + centerPoint12.x) / 2, (centerPoint03.y + centerPoint12.y) / 2);
    
    CGPoint centerPoint01 = CGPointMake((point0.x + point1.x) / 2, (point0.y + point1.y) / 2);
    CGPoint centerPoint23 = CGPointMake((point2.x + point3.x) / 2, (point2.y + point3.y) / 2);
    CGPoint centerPoint01_23 = CGPointMake((centerPoint01.x + centerPoint23.x) / 2, (centerPoint01.y + centerPoint23.y) / 2);
    
    CGPoint centerPoint02 = CGPointMake((point0.x + point2.x) / 2, (point0.y + point2.y) / 2);
    CGPoint centerPoint13 = CGPointMake((point1.x + point3.x) / 2, (point1.y + point3.y) / 2);

    if ([self.movePathForNormal containsPoint:centerPoint03_12]) {
        label.x = centerPoint03_12.x;
        label.y = centerPoint03_12.y;
    } else if ([self.movePathForNormal containsPoint:centerPoint01_23]) {
        label.x = centerPoint01_23.x;
        label.y = centerPoint01_23.y;
    } else if ([self.movePathForNormal containsPoint:centerPoint02]) {
        label.x = centerPoint02.x;
        label.y = centerPoint02.y;
    } else if ([self.movePathForNormal containsPoint:centerPoint13]) {
        label.x = centerPoint13.x;
        label.y = centerPoint13.y;
    } else {
        label.x = centerPoint03_12.x;
        label.y = centerPoint03_12.y;
    }
    
   
    
}
// 设置角度的位置
- (void)adjustAngleLabelFrame:(UILabel *)label point:(NSValue *)centerValue point1:(NSValue *)point1Value point2:(NSValue *)point2Value {
    CGPoint center = [centerValue CGPointValue];
    CGPoint point1 = [point1Value CGPointValue];
    CGFloat angle1 = 0;
    CGFloat angle2 = 0;
    CGFloat angle = 0;
    CGFloat margin = 12;
    CGPoint needPoint = center;
    // 点1在第1象限
    if (point1.x >= center.x && point1.y <= center.y) {
        angle1 = [self needAngle:atan((point1.y - center.y) / (point1.x - center.x))];
        CGPoint point2 = [point2Value CGPointValue];
        // 点2在第1象限（需要比较angle1和angle2的大小）
        if (point2.x >= center.x && point2.y <= center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))];
            angle = (angle1 + angle2) / 2;
            if (angle1 > angle2) {  // 小角
            } else {                // 大角
                angle += M_PI;
            }
        }
        // 点2在第2象限
        if (point2.x <= center.x && point2.y <= center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))];
            angle = (angle1 + angle2) / 2 + M_PI;
        }
        // 点2在第3象限
        if (point2.x < center.x && point2.y > center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))] + M_PI;
            angle = (angle1 + angle2) / 2 + M_PI;
        }
        // 点2在第4象限
        if (point2.x > center.x && point2.y > center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))] + M_PI;
            angle = (angle1 + angle2) / 2 + M_PI;
        }
        
        NSLog(@"%lf,%lf,%lf", angle1 / M_PI * 180, angle2 / M_PI * 180, angle / M_PI * 180);
        needPoint = CGPointMake(needPoint.x + (margin + label.width / 2) * cos(angle), needPoint.y - (margin + label.width / 2) * sin(angle));
    }
    if (point1.x <= center.x && point1.y <= center.y) {

        angle1 = [self needAngle:atan((point1.y - center.y) / (point1.x - center.x))];
        CGPoint point2 = [point2Value CGPointValue];
        // 点2在第1象限（需要比较angle1和angle2的大小）
        if (point2.x >= center.x && point2.y <= center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))];
            angle = (angle1 + angle2) / 2;
        }
        // 点2在第2象限
        if (point2.x <= center.x && point2.y <= center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))];
            angle = (angle1 + angle2) / 2;
            if (angle1 > angle2) {
            } else {
                angle += M_PI;
            }
        }
        // 点2在第3象限
        if (point2.x < center.x && point2.y > center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))] + M_PI;
            angle = (angle1 + angle2) / 2 + M_PI;
        }
        // 点2在第4象限
        if (point2.x > center.x && point2.y > center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))] + M_PI;
            angle = (angle1 + angle2) / 2 + M_PI;
        }
        
        NSLog(@"%lf,%lf,%lf", angle1 / M_PI * 180, angle2 / M_PI * 180, angle / M_PI * 180);
        needPoint = CGPointMake(needPoint.x + (margin + label.width / 2) * cos(angle), needPoint.y - (margin + label.width / 2) * sin(angle));
    }
    if (point1.x < center.x && point1.y > center.y) {
        
        angle1 = [self needAngle:atan((point1.y - center.y) / (point1.x - center.x))] + M_PI;
        CGPoint point2 = [point2Value CGPointValue];
        // 点2在第1象限（需要比较angle1和angle2的大小）
        if (point2.x >= center.x && point2.y <= center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))];
            angle = (angle1 + angle2) / 2;
        }
        // 点2在第2象限
        if (point2.x <= center.x && point2.y <= center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))];
            angle = (angle1 + angle2) / 2;
        }
        // 点2在第3象限
        if (point2.x < center.x && point2.y > center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))] + M_PI;
            angle = (angle1 + angle2) / 2;
            if (angle1 > angle2) {
            } else {
                angle += M_PI;
            }
        }
        // 点2在第4象限
        if (point2.x > center.x && point2.y > center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))] + M_PI;
            angle = (angle1 + angle2) / 2 + M_PI;
        }
        
        NSLog(@"%lf,%lf,%lf", angle1 / M_PI * 180, angle2 / M_PI * 180, angle / M_PI * 180);
        needPoint = CGPointMake(needPoint.x + (margin + label.width / 2) * cos(angle), needPoint.y - (margin + label.width / 2) * sin(angle));
    }
    if (point1.x > center.x && point1.y > center.y) {
        
        angle1 = [self needAngle:atan((point1.y - center.y) / (point1.x - center.x))] + M_PI;
        CGPoint point2 = [point2Value CGPointValue];
        // 点2在第1象限（需要比较angle1和angle2的大小）
        if (point2.x >= center.x && point2.y <= center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))];
            angle = (angle1 + angle2) / 2;
        }
        // 点2在第2象限
        if (point2.x <= center.x && point2.y <= center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))];
            angle = (angle1 + angle2) / 2;
        }
        // 点2在第3象限
        if (point2.x < center.x && point2.y > center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))] + M_PI;
            angle = (angle1 + angle2) / 2;
        }
        // 点2在第4象限
        if (point2.x > center.x && point2.y > center.y) {
            angle2 = [self needAngle:atan((point2.y - center.y) / (point2.x - center.x))] + M_PI;
            angle = (angle1 + angle2) / 2;
            if (angle1 > angle2) {
            } else {
                angle += M_PI;
            }
        }
        
        NSLog(@"%lf,%lf,%lf", angle1 / M_PI * 180, angle2 / M_PI * 180, angle / M_PI * 180);
        needPoint = CGPointMake(needPoint.x + (margin + label.width / 2) * cos(angle), needPoint.y - (margin + label.width / 2) * sin(angle));
    }
    
    label.x = needPoint.x;
    label.y = needPoint.y;
}

- (CGFloat)needAngle:(CGFloat)angle {
    NSLog(@"%lf", angle / M_PI * 180);
    if (angle >= -M_PI / 2 && angle <= 0) {
        angle = -angle;
        return angle;
    }
    if (angle >= 0 && angle <= M_PI / 2) {
        angle = M_PI - angle;
        return angle;
    }

    return angle;
    
}


@end
